﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace KTDSMS
{
    public class StringHash
    {
        private string PasswordHashKey = "hoangdl";
        private string SaltKey = "SQL@&YKE";
        private string VIKey = "hg#trYuDhgtr2&*(";

        public string Encrypt(string plainText)
        {
            byte[] bytes1 = Encoding.UTF8.GetBytes(plainText);
            byte[] bytes2 = new Rfc2898DeriveBytes(this.PasswordHashKey, Encoding.ASCII.GetBytes(this.SaltKey)).GetBytes(32);
            RijndaelManaged rijndaelManaged = new RijndaelManaged();
            rijndaelManaged.Mode = CipherMode.CBC;
            rijndaelManaged.Padding = PaddingMode.Zeros;
            ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(bytes2, Encoding.ASCII.GetBytes(this.VIKey));
            byte[] inArray;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(bytes1, 0, bytes1.Length);
                    cryptoStream.FlushFinalBlock();
                    inArray = memoryStream.ToArray();
                    cryptoStream.Close();
                }
                memoryStream.Close();
            }
            string s = Convert.ToBase64String(inArray);
            return s;
        }

        public string Decrypt(string encryptedText)
        {
            string str = "";
            if (this.IsBase64String(encryptedText))
            {
                byte[] buffer = Convert.FromBase64String(encryptedText);
                byte[] bytes = new Rfc2898DeriveBytes(this.PasswordHashKey, Encoding.ASCII.GetBytes(this.SaltKey)).GetBytes(32);
                RijndaelManaged rijndaelManaged = new RijndaelManaged();
                rijndaelManaged.Mode = CipherMode.CBC;
                rijndaelManaged.Padding = PaddingMode.None;
                ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(bytes, Encoding.ASCII.GetBytes(this.VIKey));
                MemoryStream memoryStream = new MemoryStream(buffer);
                CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read);
                byte[] numArray = new byte[buffer.Length];
                int count = cryptoStream.Read(numArray, 0, numArray.Length);
                memoryStream.Close();
                cryptoStream.Close();
                str = Encoding.UTF8.GetString(numArray, 0, count).TrimEnd("\0".ToCharArray());
            }
            return str;
        }

        public bool IsBase64String(string s)
        {
            s = s.Trim();
            return s.Length % 4 == 0 && Regex.IsMatch(s, "^[a-zA-Z0-9\\+/]*={0,3}$", RegexOptions.None);
        }
    }
}
